package com.cnswhy.cloud.server.merchant.controller.app;

import com.aliyun.oss.OSSClient;
import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.common.core.base.controller.SuperController;
import com.cnswhy.cloud.common.core.exception.BizException;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.common.core.utils.Kits;
import com.cnswhy.cloud.common.redis.service.IdGeneratorService;
import com.cnswhy.cloud.server.common.entity.enumeration.WalletType;
import com.cnswhy.cloud.server.core.dto.AppMenuPageDTO;
import com.cnswhy.cloud.server.core.dto.AppMenuSaveDTO;
import com.cnswhy.cloud.server.core.dto.AppMenuUpdateDTO;
import com.cnswhy.cloud.server.core.entity.AppMenu;
import com.cnswhy.cloud.server.core.entity.Customer;
import com.cnswhy.cloud.server.core.service.app.AppMenuService;
import com.cnswhy.cloud.server.merchant.util.OssUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import com.github.zuihou.security.annotation.PreAuth;


/**
 * <p>
 * 前端控制器
 * 
 * </p>
 *
 * @author cnswhy
 * @date 2020-10-22
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/appMenu")
@Api(value = "AppMenu", tags = "")
//@PreAuth(replace = "appMenu:")
public class AppMenuController extends SuperController<AppMenuService, Long, AppMenu, AppMenuPageDTO, AppMenuSaveDTO, AppMenuUpdateDTO> {

    @Autowired
    IdGeneratorService idGeneratorService;
    @ApiOperation(value = "同步图片")
    @PostMapping("/syncUrl")
    public R<Boolean> syncUrl() {
        List<AppMenu> appMenus = baseService.list();
        for(AppMenu appMenu : appMenus){
            if(StringUtils.isBlank(appMenu.getUrl())){

                appMenu.setUrl(OssUtils.updateLoadFileToOss("appmenu", appMenu.getImgUrl() ,idGeneratorService.genCommonIdString() + ".jpg"));
                baseService.updateById(appMenu);
            }
        }
        return success();
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<AppMenu> appMenuList = list.stream().map((map) -> {
            AppMenu appMenu = AppMenu.builder().build();
            return appMenu;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(appMenuList));
    }
}
